﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace RpcGenerator.AS3
{
    class AS3EnumGenerator
    {
        public static void Generate(MessageFieldDescription description, IMessageSerializer serializer)
        {
            BeginClass(description, serializer);
                WriteFields(description, serializer);
            EndClass(serializer);
        }

        private static void BeginClass(MessageFieldDescription description, IMessageSerializer serializer)
        {
            serializer.NewLine(string.Format("public class {0}", description.Type.Name));
            serializer.BeginBlock();
        }

        private static void EndClass(IMessageSerializer serializer)
        {
            serializer.EndBlock();
        }

        private static void WriteFields(MessageFieldDescription description, IMessageSerializer serializer)
        {
            foreach (FieldInfo fi in description.Type.GetFields(BindingFlags.Static | BindingFlags.Public))
            {
                int iValue = Convert.ToInt32(fi.GetValue(null));
                serializer.NewLine(string.Format("public static const {0}:int={1};", fi.Name, iValue));
            }
        }
    }
}
